﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.Data;

namespace Control
{
    public class AXCombobox : ComboBoxEdit
    {
        private DataTable _Datasource;

        public DataTable Datasource
        {
            get { return _Datasource; }
            set { _Datasource = value; }
        }
        private string _DisplayField;

        public string DisplayField
        {
            get { return _DisplayField; }
            set { _DisplayField = value; }
        }
        private string _ValueFiled;

        public string ValueFiled
        {
            get { return _ValueFiled; }
            set { _ValueFiled = value; }
        }

        private List<object[]> _List = new List<object[]>();
        //contrutor
        public AXCombobox() { }

        //Method fat sinh
        public void _Init()
        {
            base.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            _List.Clear();
            base.Properties.Items.Clear();
            foreach (DataRow row in this._Datasource.Rows)
            {
                int index = base.Properties.Items.Add(row[this._DisplayField]);
                object[] objs = new object[] { index, row[this._ValueFiled] };
                _List.Add(objs);
            }
        }

        public string _GetselectedID()
        {
            List<string> ID_Arr = new List<string>();
            string a = "";
            foreach (string item in base.Properties.Items)
            {
                if (item.ToString() != null)
                {
                    foreach (object[] objs in this._List)
                    {
                        if ((int)objs[0] == base.Properties.Items.IndexOf(item))
                        {
                            a = objs[1].ToString();
                            ID_Arr.Add(a.ToString());
                            if (base.SelectedIndex <= ID_Arr.Count - 1 && SelectedIndex >=0)
                                return ID_Arr[base.SelectedIndex];
                        }
                    }
                }
            }
            if (base.SelectedIndex == -1)
                return "";
            return ID_Arr[base.SelectedIndex];
        }

        public void _SetselectedID(string ID)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                foreach (object[] objs in this._List)
                {
                    if (objs[1].ToString() == ID)
                    {
                        builder.Append(base.Properties.Items[(int)objs[0]]);
                        //if (ID>0)
                        //    builder.Append(", ");
                        if (ID.Trim() != "")
                            this.EditValue = builder.ToString();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
    }
}
